package cn.yy.common.config;

import cn.yy.common.properties.CorsConfigProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@ConditionalOnProperty("yy.config.cors.mapping")
public class WebCorsAutoConfig implements WebMvcConfigurer {

    @Autowired
    private CorsConfigProperties corsConfigProperties;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping(corsConfigProperties.getMapping())
                .allowedOrigins(corsConfigProperties.getAllowedOrigins())
                .allowedMethods(corsConfigProperties.getAllowedMethods())
                .allowedHeaders(corsConfigProperties.getAllowedHeaders())
                .allowCredentials(corsConfigProperties.isAllowCredentials())
                .maxAge(corsConfigProperties.getMaxAge());

//         		registry.addMapping("/**")
//				.allowedOrigins("*")
//				.allowedMethods("GET","POST","PUT","OPTIONS","DELETE","PATCH")
//				.allowedHeaders("*")
//				.allowCredentials(false)
//				.maxAge(3600);
    }
}
